package com.sfh.cp.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sfh.cp.R;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述:刷新加载更多控件
 *
 * @date 2017/7/12
 */
public class WidgetMultiSelectPictureLayout extends RecyclerView implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.OnItemChildClickListener {


    /***
     * 点击回调
     */
    public interface OnItemSelectListener {

        /**
         * 选择图片
         */
        void onClickSelect(int size);

        /**
         * 查看大图
         */
        void onClickLook(int position, String data);

        void onClickDel(int position, String data);
    }

    protected int maxSize = 5;

    protected int rowSize;

    // 适配器
    protected DeliverGoodsAdvanceAdapter pullToRefreshAdapter;

    protected OnItemSelectListener listener;

    private final static String ADD = "-1";

    private int width;

    public List<String> getDataList() {

        // 去除 + 数据
        List<String> data = new ArrayList<> (5);
        for (String image : pullToRefreshAdapter.getData ()) {
            if (!TextUtils.equals (ADD, image)) {
                data.add (image);
            }
        }
        return data;
    }

    public WidgetMultiSelectPictureLayout(@NonNull Context context) {

        super (context);
    }

    public WidgetMultiSelectPictureLayout(@NonNull Context context, @Nullable AttributeSet attrs) {

        super (context, attrs);
    }

    public WidgetMultiSelectPictureLayout(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {

        super (context, attrs, defStyleAttr);
    }


    /***
     *
     * @param maxSize 最大数据
     * @param rowSize 每行数量
     * @param listener
     */
    public void setAdapter(int maxSize, int rowSize, OnItemSelectListener listener) {

        this.maxSize = maxSize;
        this.rowSize = rowSize;
        this.listener = listener;
        this.pullToRefreshAdapter = new DeliverGoodsAdvanceAdapter (maxSize + 1);
        GridLayoutManager gridLayoutManager = new GridLayoutManager (getContext (), rowSize);
        this.setAdapter (pullToRefreshAdapter);
        this.setLayoutManager (gridLayoutManager);
        this.pullToRefreshAdapter.setOnItemClickListener (this);
        this.pullToRefreshAdapter.setOnItemChildClickListener (this);
        this.addEmpty ();
    }

    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {

        super.onMeasure (widthSpec, heightSpec);
        width = getMeasuredWidth () / rowSize - 15;
        pullToRefreshAdapter.notifyDataSetChanged ();
    }


    /***
     * 1.添加图片
     */
    public void onAdd(List<String> data) {

        if (data == null) {
            return;
        }
        // 有效数据-多一个+
        int dataSize = this.pullToRefreshAdapter.getData ().size () - 1;
        int tempSize = data.size ();

        if (dataSize + tempSize > maxSize) {

            //数据超出
            return;
        }
        this.pullToRefreshAdapter.getData ().remove (ADD);
        this.pullToRefreshAdapter.addData (data);


        dataSize = this.pullToRefreshAdapter.getData ().size ();
        if (dataSize >= maxSize) {
            return;
        }
        this.addEmpty ();
    }

    /***
     * 1.添加图片
     */
    public void onReplaceData(List<String> data) {

        if (data == null) {
            return;
        }

        int tempSize = data.size ();

        if (tempSize > maxSize) {

            this.pullToRefreshAdapter.replaceData (data.subList (0, maxSize - 1));
        } else {
            this.pullToRefreshAdapter.replaceData (data);
        }


        int dataSize = this.pullToRefreshAdapter.getData ().size ();
        if (dataSize >= maxSize) {
            return;
        }
        this.addEmpty ();
    }

    /***
     * 1.添加图片
     */
    public void onAdd(String image) {

        // 最后一条数据量
        int dataSize = pullToRefreshAdapter.getData ().size () - 1;

        if (dataSize >= maxSize) {
            return;
        }

        pullToRefreshAdapter.getData ().remove (ADD);
        pullToRefreshAdapter.getData ().add (image);

        // 最多数量
        dataSize = pullToRefreshAdapter.getData ().size ();
        if (dataSize >= maxSize) {
            return;
        }

        this.addEmpty ();
    }

    /***
     *   2.删除图片
     */
    public void onDel(String data) {

        int oldIndex = this.pullToRefreshAdapter.getData ().indexOf (data);
        final int size = this.pullToRefreshAdapter.getData ().size ();

        if (oldIndex != -1 && oldIndex < size) {
            this.pullToRefreshAdapter.remove (oldIndex);
        }
        this.addEmpty ();
    }

    /***
     *   2.删除图片
     */
    public void onDel(int position) {

        final int size = this.pullToRefreshAdapter.getData ().size ();

        if (position != -1 && position < size) {
            this.pullToRefreshAdapter.remove (position);
        }
        this.addEmpty ();
    }

    private void addEmpty() {

        final int size = pullToRefreshAdapter.getData ().size ();
        if (size == 0) {
            pullToRefreshAdapter.addData (ADD);
            return;
        }

        final int lastIndex = size - 1;
        String lastData = pullToRefreshAdapter.getItem (lastIndex);
        // 最后一个是空数据
        if (TextUtils.equals (ADD, lastData)) {
            return;
        }

        if (size < maxSize) {
            pullToRefreshAdapter.addData (ADD);
        }

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

        if (listener == null) {
            return;
        }
        String mData = (String) adapter.getItem (position);
        if (TextUtils.equals ("-1", mData) || TextUtils.isEmpty (mData)) {
            // 添加图片
            int size = maxSize - adapter.getData ().size () + 1;
            listener.onClickSelect (size);
        } else {
            //查看
            listener.onClickLook (position, mData);
        }

    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

        if (listener == null) {
            return;
        }
        //删除
        String mData = (String) adapter.getItem (position);
        listener.onClickDel (position, mData);
    }

    class DeliverGoodsAdvanceAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public DeliverGoodsAdvanceAdapter(int size) {

            super (R.layout.widget_multi_select_item, new ArrayList<String> (size));
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {

            ImageView iv = helper.getView (R.id.ivItem);

            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) iv.getLayoutParams ();
            params.width = width;
            params.height = width;
            iv.setLayoutParams (params);

            // 删除
            helper.addOnClickListener (R.id.ivOption);

            if (TextUtils.equals (ADD, item) || TextUtils.isEmpty (item)) {
                // 添加图片
                helper.setVisible (R.id.ivOption, false);
                iv.setImageResource (R.mipmap.dz_publish_add_picture_n);
            } else {
                helper.setVisible (R.id.ivOption, true);
                Picasso.with (getContext ())
                        .load (item).config (Bitmap.Config.RGB_565)
                        .into (iv);
            }

        }
    }
}
